package com.nine.news.util;

import cn.hutool.http.HttpUtil;
import org.apache.commons.io.IOUtils;
import org.json.JSONObject;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: szhan
 * @Date: 2023/03/17/8:29 AM
 * @Description:
 */
public class FaceUtil {

    /**
     * 获取令牌  有效期30天
     * @return 对应的令牌  有效期30天*/
    public static String getToken(){
        try {
            //1.创建连接对象
            HttpURLConnection connection= (HttpURLConnection) new URL("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials").openConnection();
            //2.设置请求信息
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            OutputStream os=connection.getOutputStream();
            os.write("client_id=ersxsB44om07AXnMlYOUuyxM&client_secret=958GmvwoeC0iqeHEw9usDOmVuv8hSAOf".getBytes());
            os.flush();
            //3.连接
            connection.connect();
            //4.验证请求的结果
            if(connection.getResponseCode()==200){
                //5.获取响应的结果
                String res= IOUtils.toString(connection.getInputStream());
                JSONObject object=new JSONObject(res);
                return object.getString("access_token");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 人脸特征分析
     * @param img 要检测的脸*/
    public static String getDetect(String img){
        //1.准备参数
        Map<String,String> param=new HashMap<>();
        param.put("image_type","BASE64");
        param.put("face_field","age,eye_status,emotion,gender,glasses,quality");
        param.put("image",img);
        //2.发起请求
        String res=HttpUtil.post("https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=24.bbc3355fea35c154841d877056a9fafb.2592000.1681369880.282335-17540674",
                new JSONObject(param).toString());
        return res;
    }
    /**
     * 人脸注册
     * @param img 要注册的人脸
     * @param uid 用户id*/
    public static String register(String img,int uid){
        //1.准备参数
        Map<String,Object> param=new HashMap<>();
        param.put("image_type","BASE64");
        param.put("group_id","company001");
        param.put("user_id",uid);
        param.put("image",img);
        //2.发起请求
        String res=HttpUtil.post("https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add?access_token=24.bbc3355fea35c154841d877056a9fafb.2592000.1681369880.282335-17540674",
                new JSONObject(param).toString());
        return res;
    }
    /**
     * 人脸搜索
     * @param img 要搜索的人脸*/
    public static String search(String img){
        //1.准备参数
        Map<String,Object> param=new HashMap<>();
        param.put("image_type","BASE64");
        param.put("group_id_list","company001");
        param.put("image",img);
        //2.发起请求
        String res=HttpUtil.post("https://aip.baidubce.com/rest/2.0/face/v3/search?access_token=24.bbc3355fea35c154841d877056a9fafb.2592000.1681369880.282335-17540674",
                new JSONObject(param).toString());
        return res;
    }
    /**
     * 图片转base64*/
    public static String img2Base64(String url) throws IOException {
        return Base64.getEncoder().encodeToString(IOUtils.toByteArray(new FileInputStream(url)));
    }

}
